package com.example.teesystem.mapper;

import com.example.teesystem.entity.IssueRecord;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.teesystem.entityDto.StudentAnswerMessage;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
* @author 12510
* @description 针对表【t_issue_record】的数据库操作Mapper
* @createDate 2022-04-14 19:38:55
* @Entity com.example.teesystem.entity.IssueRecord
*/
@Mapper
public interface IssueRecordMapper extends BaseMapper<IssueRecord> {
    @Update("update t_issue_record set right_num = right_num+1,answer = #{answer} where id = #{id} and student_id = #{studentId}")
    void addRightNum(int id, int studentId,String answer);

    @Update("update t_issue_record set wrong_num = wrong_num+1,answer = #{answer} where id = #{id} and student_id = #{studentId}")
    void addWrongNum(int id, int studentId,String answer);

    @Select("select id from t_issue_record where issue_id = #{issueId} and student_id = #{studentId}")
    int getIdByMsg(int issueId, int studentId);

    @Select("select * from t_issue_record where student_id = #{studentId}")
    List<IssueRecord> getStudentRecorde(int studentId);

    @Select("select sum(right_num) from t_issue_record where student_id = #{studentId}")
    int getCorrectNum(int studentId);

    @Select("select sum(wrong_num) from t_issue_record where student_id = #{studentId}")
    int getWrongNum(int studentId);

    @Select("select * from t_issue_record where issue_id = #{issueId}")
    List<IssueRecord> getRecordByIssueId(int issueId);

    @Select({
            "<script>"+
            "select * from t_issue_record where student_id = #{studentId} and issue_id in " +
            "<foreach item = 'item' index = 'index' collection = 'ids' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>"+
            "</script>"
    })
    List<IssueRecord> getRecordsByStudentIdAndIssueIds(int studentId, @Param("ids") List<Integer> ids);

    @Select("select * from t_issue_record where student_id = #{studentId} and issue_id = #{issueId}")
    IssueRecord getRecordByIssueIdAndStudentId(int issueId, int studentId);

    @Select("SELECT issue_id FROM " +
            "(select issue_id, sum(wrong_num)/(sum(wrong_num)+sum(right_num)) as allNum from t_issue_record where course_id = #{courseId} GROUP BY issue_id) as temp " +
            "where temp.allNum>0.5 order by rand() limit 20")
    List<Integer> getIssueIdByWrongNum(int courseId);

    @Select("select * from t_issue_record where student_id = #{studentId} and course_Id = #{courseId}")
    List<IssueRecord> getStudentRadarMap(int studentId,int courseId);

    @Select("select sum(right_num) as 'right',sum(wrong_num) as wrong from t_issue_record  where course_id = #{courseId} and student_id = #{studentId}")
    StudentAnswerMessage getStudentAnswerMessage(int courseId,int studentId);
}




